<?php

namespace App\Http\Controllers;

use App\Video;
use App\Http\Requests\VideoRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class VideoController extends Controller
{
    /**
     * Display a listing of the videos
     *
     * @return \Illuminate\View\View
     */
    public function index(Request $request, Video $model)
    {
        $model = $model->where('club_id', $request->user()->club_id);

        if (is_null($request->search))
            // 按分类查看列表
            $videos = is_null($request->type)
                ? $model->paginate(15)
                : $model->where('status', $request->type)->paginate(15);
        else
            // 搜索
            $videos = $model->where('title', 'like', "%{$request->search}%")
                ->orWhere('intro', 'like', "%{$request->search}%")
                ->paginate(15);

        return view('videos.index', [
            'videos' => $videos,
            'countAll' => $videos->count(),
            'countStatus0' => $videos->where('status', 0)->count(),
            'countStatus1' => $videos->where('status', 1)->count(),
            'countStatus2' => $videos->where('status', 2)->count(),
        ]);
    }

    /**
     * Show the form for creating a new video
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        return view('videos.create');
    }

    /**
     * Store a newly created video in storage
     *
     * @param \App\Http\Requests\VideoRequest $request
     * @param \App\Video $model
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(VideoRequest $request, Video $video)
    {
        $videos = [];
        foreach ($request->videoFiles as $file) {
            array_push($videos, (object)[
                'videoName' => $file->getClientOriginalName(),
                'path' => preg_replace('/^public\//', '/storage/', $file->store('public/vidoes')),
                'mimeType' => $file->getClientMimeType(),
                'size' => $file->getClientSize(),
                'active' => true
            ]);
        }

        $video->create($request->merge([
            'club_id' => $request->user()->club_id,
            'admin_id' => $request->user()->id,
            'status' => Video::STATUS_DRAFT,
            'videos' => $videos
        ])->all());

        return redirect()->route('video.index')->withStatus('视频课程创建成功');
    }

    /**
     * Show the form for editing the specified user
     *
     * @param \App\Video $video
     * @return \Illuminate\View\View
     */
    public function edit(Video $video)
    {
        if(Auth::user()->club_id!==$video->club_id)
            abort('403');
        return view('videos.edit', compact('video'));
    }

    /**
     * Update the specified user in storage
     *
     * @param \App\Http\Requests\UserRequest $request
     * @param \App\User $user
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(VideoRequest $request, Video $video)
    {
        $video->setVideosActive($request->oldVideos);
        $videosToAdd = [];
        foreach ($request->videoFiles as $file) {
            array_push($videosToAdd, (object)[
                'videoName' => $file->getClientOriginalName(),
                'path' => preg_replace('/^public\//', '/storage/', $file->store('public/vidoes')),
                'mimeType' => $file->getClientMimeType(),
                'size' => $file->getClientSize(),
                'active' => true
            ]);
        }
        $video->addVideos($videosToAdd);
        $video->update($request->only(['title', 'intro', 'price']));

        return redirect()->route('video.index')->withStatus('修改成功');
    }

    public function status(Request $request, $id)
    {
        $video = Video::findOrFail($id);
        $video->status = $request->get('status');
        if ($request->get('status') == 1)
            $video->published_at = now();
        $video->save();
        return response(json_encode((object)[
            'status' => $video->status,
            'published_at' => $video->published_at->toDateTimeString()
        ]));
    }

}
